<template>
  <el-card>
    <el-table border style="margin: 10px 0" :data="skuData">
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column
        label="名称"
        width="200px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        width="200px"
        prop="skuDesc"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="图片" width="150px">
        <template #="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt="sku图片"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        width="150px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格"
        width="150px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" width="280px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            :type="row.isSale ? 'info' : 'success'"
            :icon="row.isSale ? 'Bottom' : 'Top'"
            plain
            @click="updateSale(row)"
            v-has="`btn.Sku.updown`"
          ></el-button>
          <el-button
            type="warning"
            icon="Edit"
            plain
            @click="updateSku"
            v-has="`btn.Sku.update`"
          ></el-button>
          <el-button
            type="primary"
            icon="InfoFilled"
            plain
            @click="info(row)"
            v-has="`btn.Sku.detail`"
          ></el-button>
          <el-popconfirm
            :title="`你确定删除'${row.skuName}'吗？`"
            @confirm="deleteSku(row.id)"
            width="250"
          >
            <template #reference>
              <el-button
                type="danger"
                icon="Delete"
                plain
                v-has="`btn.Sku.remove`"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15]"
      layout="prev, pager, next, jumper,->,total, sizes"
      :total="Number(total)"
      @size-change="handleSizeChange"
      @current-change="getSkuData"
      :pager-count="7"
      :hide-on-single-page="true"
    />

    <el-drawer v-model="drawer">
      <template #header>
        <h4>商品详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ infoData.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ infoData.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ infoData.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in infoData.skuAttrValueList"
              :key="item.id"
              style="margin: 0 5px"
            >
              {{ item.attrName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in infoData.skuSaleAttrValueList"
              :key="item.id"
              style="margin: 0 5px"
            >
              {{ item.saleAttrName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <template v-if="infoData.skuImageList.length > 0">
              <el-carousel trigger="click" height="200px">
                <el-carousel-item
                  v-for="item in infoData.skuImageList"
                  :key="item.id"
                >
                  <img
                    :src="item.imgUrl"
                    :alt="item.imgName"
                    style="width: 100%; height: 100%"
                  />
                </el-carousel-item>
              </el-carousel>
            </template>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import {
  getSkuList,
  onSale,
  cancelSale,
  productInfo,
  deleteSKU,
} from '@/api/product/sku/index.ts'
import { ElMessage } from 'element-plus'
const pageNo = ref(1)
const pageSize = ref(5)
const total = ref()
const skuData = ref()
const drawer = ref(false)
const infoData = ref()
onMounted(() => {
  getSkuData()
})
const getSkuData = async (pager = 1) => {
  pageNo.value = pager
  let res = await getSkuList(pageNo.value, pageSize.value)
  if (res.code === 200) {
    skuData.value = res.data.records
    total.value = res.data.total
  }
}
const handleSizeChange = () => {
  getSkuData()
}
const updateSale = async (row) => {
  if (row.isSale) {
    let res = await onSale(row.id)
    if (res.code === 200) {
      ElMessage({
        type: 'success',
        message: '下架成功',
      })
      getSkuData(pageNo.value)
    } else {
      ElMessage({
        type: 'error',
        message: '下架失败',
      })
    }
  } else {
    let res = await cancelSale(row.id)
    if (res.code === 200) {
      ElMessage({
        type: 'success',
        message: '上架成功',
      })
      getSkuData(pageNo.value)
    } else {
      ElMessage({
        type: 'error',
        message: '上架失败',
      })
    }
  }
}
const updateSku = () => {
  ElMessage({
    type: 'warning',
    message: '功能还在开发中......',
  })
}
const info = async (row) => {
  let res = await productInfo(row.id)
  if (res.code === 200) {
    infoData.value = res.data
    drawer.value = true
  }
}
const deleteSku = async (id) => {
  let res = await deleteSKU(id)
  if (res.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    await getSkuData(skuData.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>

<style lang="scss" scoped>
.demonstration {
  color: var(--el-text-color-secondary);
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
